package fr.ants.view;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

/**
 * @author Guillaume Ruscassie
 * 
 */
public class PictureManage {
	/**
	 * Build a small icon
	 * 
	 * @param path
	 * @return {@link ImageIcon}
	 */
	public static ImageIcon createIconTree(String path) {
		ImageIcon icon = new ImageIcon(Items.class.getResource(path));
		return new ImageIcon(getScaledImage(icon.getImage(), 16, 16));

	}

	/**
	 * Build a icon for the tool bar
	 * 
	 * @param path
	 * @return {@link ImageIcon}
	 */
	public static ImageIcon createIconToolBar(String path) {
		ImageIcon icon = new ImageIcon(Items.class.getResource(path));
		return new ImageIcon(getScaledImage(icon.getImage(), 32, 32));
	}

	/**
	 * Build a icon for the popup menu bar
	 * 
	 * @param path
	 * @return {@link ImageIcon}
	 */
	public static ImageIcon createIconPopupMenu(String path) {
		ImageIcon icon = new ImageIcon(Items.class.getResource(path));
		return new ImageIcon(getScaledImage(icon.getImage(), 24, 24));

	}

	/**
	 * to create a icon with a size
	 * 
	 * @param path
	 * @param width
	 * @param height
	 * @return ImageIcon
	 */
	public static ImageIcon createIcon(String path, int width, int height) {
		ImageIcon icon = new ImageIcon(Items.class.getResource(path));
		return new ImageIcon(getScaledImage(icon.getImage(), width, height));

	}

	/**
	 * Resizes an image using a Graphics2D object backed by a BufferedImage and
	 * to use the translucent
	 * 
	 * @param srcImg
	 *            - source image to scale
	 * @param width
	 *            - desired width
	 * @param height
	 *            - desired height
	 * @return - the new resized image
	 */
	private static Image getScaledImage(Image srcImg, int width, int height) {
		BufferedImage buffImg = new BufferedImage(width, height,
				Transparency.TRANSLUCENT);

		Graphics2D g2 = buffImg.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.drawImage(srcImg, 0, 0, width, height, null);
		g2.dispose();
		return buffImg;
	}

}
